package com.offcn.servlet;

import com.offcn.pojo.Orders;
import com.offcn.pojo.User;
import com.offcn.service.CourseUserService;
import com.offcn.util.ResultVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

/*支付模块*/
@WebServlet("/order")
public class OrderServlet extends BaseServlet {

    private CourseUserService courseUserService = new CourseUserService();
    private ResultVo vo = null;

    /*支付功能*/
    public void pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求中的参数
        Integer cid = Integer.valueOf(request.getParameter("cid"));
        Double sumPrice = Double.valueOf(request.getParameter("sumPrice"));
        //订单编号：使用随机数生成
        String orderNum = UUID.randomUUID().toString();
        //订单名称：登录人姓名+登录账号
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("adminUser");
        String orderName = user.getName() + "_" + user.getUsername() + "的订单";
        //登录人id
        Integer uid = user.getUid();
        //将用户课程保存大选课信息表中
        int row = courseUserService.insertCourseUser(cid, uid, orderName);
        //封装订单对象
        Orders orders = new Orders(orderNum, orderName, sumPrice);
        if (row > 0) {
            //跳转支付页面
            request.setAttribute("orders",orders);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }
    }

    /*根据订单编号修改状态*/
    public void updateFlag(HttpServletRequest request,HttpServletResponse response){
        //获取订单编号
        String orderNum = request.getParameter("orderNum");
        //执行修改
        CourseUserService.updateFlag(orderNum);//service为未完成
    }
}
